package com.gilight.base.controller.uiapp;

import com.gilight.base.aspect.annotation.Auth;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.AppServiceException;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.CouponDTO;
import com.gilight.base.dto.CouponUserDTO;
import com.gilight.base.uiapp.category.CategoryService;
import com.gilight.base.uiapp.coupon.CouponService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/app-coupon")
public class AppCouponController {

    @Autowired
    private CouponService couponService;


    /**
     *
     * @return
     */
    @ApiOperation(value = "获取当前用户机构下所有的优惠卷")
    @RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
    @Auth
    public ResultEntity categoryList(Long userId,Integer status,Long zzjgId) {
        ResultEntity resultEntity = new ResultEntity();
        /**
         * status
         * ==1 表示查询未使用优惠卷
         * ==2 表示查询已经使用优惠卷
         * ==3 表示查询未使用但已过期优惠卷
         */
        try {
            List<CouponUserDTO> userCoupons = couponService.getUserCoupons(userId,status,zzjgId);
            resultEntity.setData(userCoupons);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    /**
     *
     * @return
     */
    @ApiOperation(value = "获取当前用户所有的优惠卷")
    @RequestMapping(value = "/list-all", method = {RequestMethod.GET,RequestMethod.POST})
    @Auth
    public ResultEntity allCoupon(Long userId,Integer status) {
        ResultEntity resultEntity = new ResultEntity();
        /**
         * status
         * ==1 表示查询未使用优惠卷
         * ==2 表示查询已经使用优惠卷
         * ==3 表示查询未使用但已过期优惠卷
         */
        try {
            List<CouponUserDTO> userCoupons = couponService.getAllUserCoupons(userId,status);
            resultEntity.setData(userCoupons);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    /**
     *当新用户通过老用户分享链接并注册成功时的一个绑定关系
     * @return
     */
    @ApiOperation(value = "标记新老用户之前的分享关系")
    @RequestMapping(value = "/mark-n-o", method = {RequestMethod.GET,RequestMethod.POST})
    @Auth
    public ResultEntity bookOldUserAndNewUser(Long oldUserId,Long newUserId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            Boolean flag = couponService.bookOldUserAndNewUser(oldUserId, newUserId);
            resultEntity.setData(flag);
        }catch (Exception e){
            resultEntity.setData(false);
        }

        return resultEntity;
    }
    @ApiOperation(value = "获取当前用户在当前店铺可领取的优惠卷")
    @RequestMapping(value = "/list-shop-coupons", method = {RequestMethod.GET,RequestMethod.POST})
    @Auth
    public ResultEntity getShopCoupon(Long zzjgId,Long userId) {
        ResultEntity resultEntity = new ResultEntity();
        List<CouponDTO> userCoupons = couponService.getObtainableCouponByShopUser(2L,zzjgId,userId);
        resultEntity.setData(userCoupons);
        return resultEntity;
    }
    @ApiOperation(value = "领取优惠卷")
    @RequestMapping(value = "/obtion-coupon", method = {RequestMethod.GET,RequestMethod.POST})
    @Auth
    public ResultEntity obtionCoupon(Long couponId,Long userId) throws AppServiceException {
        ResultEntity resultEntity = new ResultEntity();
        String flag = couponService.obtainCoupon(couponId,userId);
        resultEntity.setData(flag);
        return resultEntity;
    }
}
